﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using System.Xml;
using System.IO;

public class StartMain : MonoBehaviour {

	// Use this for initialization
    public UnityEngine.UI.Toggle m_tgIsNewPlayer;
    public Transform m_tChats;
    private Vector3 m_vDragLast,m_vBig,m_vNormal;
    private bool m_bIsMoveLeft, m_bIsMoveRight;
    private int m_iChatCount,m_iSelectChar;
    private List<bool> m_lstIsCharLock;

	void Start () {
        //PlayerPrefs.DeleteAll();
        InitialGameData();
        CommonFunctionA.PlayAudio(CommonFunctionA.m_gSelf, "StartMain");
        InitialMember();
        EventDispatcher.btnEvent += onBtnEvent;
        EventDispatcher.dropEvent += onDropEvent;
        

	}

    private void InitialGameData()
    {
        GameData.GetInstance();
    }
    private void InitialMember()
    {
        string sIsNewPlayer = PlayerPrefs.GetString("sIsNewPlayer");
        if (sIsNewPlayer == null) sIsNewPlayer = "No";
        if (sIsNewPlayer == "Yes")
        {
            m_tgIsNewPlayer.isOn = true;
        }
        else
        {
            m_tgIsNewPlayer.isOn = false;
        }
        m_vBig = new Vector3(1.5f, 1.5f, 0);
        m_vNormal = new Vector3(1, 1, 0);
        m_bIsMoveRight = false;
        m_bIsMoveLeft = true;
        m_iChatCount = m_tChats.transform.childCount;
        m_lstIsCharLock = new List<bool>();
        int iCharCount = PlayerPrefs.GetInt("iCharCount");
        CommonFunctionA.logger("icharcount:" + iCharCount, 1);
        //PlayerPrefs.SetInt("iCharCount", 0);这句用来初始化

        if (iCharCount == 0)
        {
            //初始化
            iCharCount = 6;
            PlayerPrefs.SetInt("iCharCount",6);
            for (int i = 0; i < iCharCount; i++)
            {
                if (i == 0 || i==1)
                {
                    m_lstIsCharLock.Add(true);
                    PlayerPrefs.SetInt("iChar" + i + "IsLock",1);
                }
                else
                {
                    m_lstIsCharLock.Add(false);
                    PlayerPrefs.SetInt("iChar" + i + "IsLock", 0);
                }
            }
        }
        else
        {            
            for (int i = 0; i < iCharCount; i++)
            {
                m_lstIsCharLock.Add( PlayerPrefs.GetInt("iChar" + i + "IsLock") == 1 ? true : false);
            }
        }
        int iSelectChar = PlayerPrefs.GetInt("iSelectChar");
        if (iSelectChar == 0) iSelectChar = 0;
        m_iSelectChar = iSelectChar;
        //将禁用情况与选取英雄初始化
        for (int i = 0; i < m_iChatCount; i++)
        {
            Transform t = m_tChats.transform.GetChild(i);
            if (m_lstIsCharLock[i])
            {
                //将锁隐藏
                t.GetChild(0).gameObject.SetActive(false);
            }
            else
            {
                t.GetChild(0).gameObject.SetActive(true);
            }

            //将其移至正中
            if (iSelectChar == i)
            {
                m_tChats.Translate(Screen.width / 2 - t.position.x, 0, 0);
                t.localScale = m_vBig;
                t.GetComponent<UnityEngine.UI.RawImage>().color = Color.white;
            }
            else
                t.GetComponent<UnityEngine.UI.RawImage>().color = Color.gray;
        }
    }

    // Update is called once per frame
    void Update()
    {
		
	}

    public void onBtnEvent(GameObject btn, UserOperate.btnState btnState)
    {
        if (btnState == UserOperate.btnState.btnStateUp && btn.name == "btnStart")
        {
            //当选好角色，点击开始游戏。
            PlayerPrefs.SetInt("iSelectChar",m_iSelectChar);
            GameData.pi.iChar = m_iSelectChar;
            UnityEngine.SceneManagement.SceneManager.LoadScene("Loading");
        }        
        else if (btnState == UserOperate.btnState.btnStateUp && btn.name == "StartMain_btnStart")
        {
            //当开始游戏点击开始进行角色选取
             m_tChats.parent.gameObject.SetActive(true);            
        }
        
    }

    public void onDropEvent(GameObject obj, UserOperate.dragState btnState, PointerEventData eventData)
    {
        if (obj.name == "PanelSelection")
        {
            if (btnState == UserOperate.dragState.dragStateBegin)
            {
                m_vDragLast = eventData.position;
            }
            else if (btnState == UserOperate.dragState.dragStateDraging)
            {
                //CommonFunctionA.logger("x:" + (eventData.position.x - vDragLast.x).ToString(), 1);
                //计算X轴距离即可，按比例移动obj
                
                //如果某个角色接近了屏幕中心一定范围，则将其慢慢变大
                for (int i = 0; i < m_iChatCount; i++)
                {
                    Transform t = m_tChats.transform.GetChild(i);
                    if (t.position.x <= Screen.width / 2 + Screen.width * 0.05f && t.position.x >= Screen.width / 2 - Screen.width * 0.05f)
                    {
                        m_iSelectChar = i;
                        t.GetComponent<UnityEngine.UI.RawImage>().color = Color.white;
                        t.localScale = m_vBig;
                        if (i == 0)
                            m_bIsMoveRight = false;
                        else
                            m_bIsMoveRight = true;

                        if (i == m_iChatCount-1)
                            m_bIsMoveLeft = false;
                        else
                            m_bIsMoveLeft = true;
                    }
                    else
                    {
                        t.GetComponent<UnityEngine.UI.RawImage>().color = Color.gray;
                        t.localScale = m_vNormal;
                    }                    
                }

                //如果第1个在中间了，不能再往右移；尾个在中间了不能再左移。
                if (eventData.position.x - m_vDragLast.x > 0 && m_bIsMoveRight)
                {
                    m_tChats.transform.Translate(eventData.position.x - m_vDragLast.x, 0, 0);
                }
                else if (eventData.position.x - m_vDragLast.x < 0 && m_bIsMoveLeft)
                {
                    m_tChats.transform.Translate(eventData.position.x - m_vDragLast.x, 0, 0);
                }
                m_vDragLast = eventData.position;
            }
        }
    }
}
